//@author A0096950U
#include "History.h"

const string History::ERRORMESSAGE_NO_COMMAND_TO_UNDO = "No command to undo";

History::History (void) : logger(Logger::getInstance()) {
}

// Pre: Action object is already constructed
// Post: Stores Action object in history
void History::addAction(Action actionToAdd) {

	actionHistory.push(actionToAdd);

	logger.log(LOG_INFO, "History: Added action to history.");
}

// Pre: Employs getLastAction method
// Post: Removes and returns last Action from history
//		 Throw exception if Action history is empty
Action History::undoAction() {

	Action actionToUndo = getLastAction();

	actionHistory.pop();

	logger.log(LOG_INFO, "History: Undo. Removed last action from history.");

	return actionToUndo;
}

//@author A0105511Y
// Pre: nil
// Post: Returns last Action
//		 Throws exception if Action history is empty
Action History::getLastAction() {

	if(!actionHistory.empty()) {
		return actionHistory.top();
	}
	else {
		throw runtime_error(ERRORMESSAGE_NO_COMMAND_TO_UNDO);
	}
}